<?php

namespace app\fly_pig\controller;

class FlyPigOrderComment
{

    public $count = 1;
    /**
     * 构造函数
     * MfwComment constructor.
     */
    public function __construct(){}

    /**
     *  子进程启动时触发
     * @param $worker
     */
    public function onWorkerStart($worker){
        echo "飞猪订单点评子进程启动了\n";
       if ($worker->id === 0){
           \workerman\Lib\Timer::add(30, [$this,'dealDate']);
       }elseif ($worker->id === 1){
           \workerman\Lib\Timer::add(100, [$this, 'timeTask']);
       }
    }

    /**
     * 连接时触发
     * @param $connection
     */
    public function onConnect($connection){
        echo "连接成功\n";
        $connection->send('<h2 style="color: #4288ce;font-size: 20px">hello!</h2>');
    }

    /**
     *  接收到消息时触发
     * @param $connection
     * @param $message
     */
    public function onMessage($connection, $message) {
        var_dump($message);
        $connection->send("<span style='color: #000088;'>很高兴和你认识</span>\n");
    }

    /**
     *  连接关闭时触发
     * @param $connection
     */
    public function onClose($connection){
        echo "连接断开了\n";
    }

    /**
     *  worker进程停止时触发
     * @param $connection
     */
    public function onWorkerStop($connection){
        echo "正在重启!\n";
    }


    /**
     *  从马蜂窝平台获取订单信息
     */
    public function timeTask()
    {
        echo "定时任务\n";
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(7);
        // 获取日期
        $date = $redis->lPop('order_comment_date');
        if (!empty($date)){
            $page_no = 1;
            if (strlen($date) >= 11){ // 有分页
                $totalPage = substr($date, 11); // 获取页数
                $date = substr($date, 0 ,10);
                $val = $redis->get($date);
                if (!empty($val) && ($val <= $totalPage));{
                    $page_no = $page_no + 1;
                    $redis->incr($date); // 自增1
                }
            }
            $date = strtotime($date);
            // 调用从奇门获取飞猪订单评论的接口
            $flyPigOrder = new FlyPig();
            $flyPigOrder->syncFlyPigOrderComment($date, $page_no);
        }

    }


    /**
     *  处理日期
     */
    public function dealDate()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(7);
        $date = date('Y-m-d',time());
        $dates = $this->getDates($date);
        // 从redis获取数据
        $data1 = $redis->lRange('order_comment_date', 0 , -1);
        $data2 = $redis->lRange('order_comment_date1',0, -1);
        $data = [];
        if (!empty($data1) && !empty($data2)) {
            $dateData1 = [];
            foreach ($data1 as $index => $item) {
                array_push($dateData1, substr($item, 0 ,10));
            }
            $data = array_unique(array_merge($dateData1, $data2));
        }elseif (!empty($data1) && empty($data2)){
            $dateData1 = [];
            foreach ($data1 as $index => $item) {
                array_push($dateData1, substr($item, 0 ,10));
            }
            $data = $dateData1;
        }elseif (empty($data1) && !empty($data2)){
            $data = $data2;
        }
        if (!empty($data)){
            foreach ($dates as $index1 => $datum) {
                if (!in_array($datum, $data)){
                    $redis->rPush('order_comment_date', $datum);
                }
            }
        }else{
            foreach ($dates as $index2 => $datum2) {
                $redis->rPush('order_comment_date', $datum2);
            }
        }
        // 删除列表页多余的日期
        $currentDate = date('Y-m-d', time());
        $st = $currentDate.' 00:00:00';
        $end = $currentDate.' 01:00:00';
        if ($st <= $currentDate && $currentDate <= $end){
            $currentData = $this->getDates($currentDate);
            $orderDate1 = $redis->lRange('order_comment_date1', 0, -1);
            if (!empty($currentData) && !empty($orderDate1)){
                $count = count($orderDate1);
                foreach ($orderDate1 as $index => $item) {
                    if (!in_array($item, $currentData)){
                        $redis->lRem('order_comment_date1',$item,$count);
                    }
                }
            }
        }
    }

    /**
     *  获取前10天的日期
     */
    public function getDates($value = '')
    {
        $arr = [];
        $day = 30;
        $arr[] = $value;
        if (!empty($value)){
            for ($i = 1; $i < $day; $i++) {
                $arr[] = date('Y-m-d', strtotime("{$value} -{$i}day"));
            }
        }
        return $arr;
    }

}